angular.module('tab-orderDetail', [])
 .controller 'OrderDetailCtrl', ($location,Toast, $localStorage, $ionicLoading, $scope, $state, $http,$stateParams, $window, $ionicHistory, $ionicPopup, orderService) ->
  $scope.orderId=$stateParams.id;
  console.log($scope.orderId)
  loadData = ->
   $http.get('/order/'+$scope.orderId)
    .success (resp) ->
     $scope.order=resp.message
     console.log($scope.order)
     if $scope.order.shift==1
      $scope.banci="白班"
     else if $scope.order.shift==0
      $scope.banci="全天"
     else
      $scope.banci="夜班"
     $scope.dayList=$scope.order.orderTracks
    .error (resp, status) ->
     Toast.showBusinessError(resp.message)
  loadData()
  $scope.toSure = (id)->
    myPopup = $ionicPopup.confirm({
      title: '是否确认服务完成'
      template: ''
      okText: '确认'
      cancelText: '我再想想'
      cssClass:'my-custom-popup'
    })
    myPopup.then (res) ->
      if res
        $http.put('/orderTrack/'+id, {id: id})
        .success (resp) ->
          Toast.showMsg("确认服务完成")
          $window.location.reload()
          .error (resp, status) ->
          Toast.showBusinessError(resp.message)
      else

  #返回
  $scope.back= ->
    $state.go('tab.order-list')

  _.extend $scope,
      # 提前终止订单
      ahead: (id) ->
          orderService.ahead(id).then(->
              loadData()
          )

      #取消订单
      cancel: (id) ->
          orderService.cancel(id).then(->
              loadData()
          )

      goPay: () ->
          $state.go('tab.pay',{id: $scope.order.id})